package com.unitd.modules.sso.controller;

import com.unitd.comm.web.BaseController;
import com.unitd.frame.comm.utils.BeanUtils;
import com.unitd.frame.sso.annotation.Action;
import com.unitd.frame.sso.annotation.Permission;
import com.unitd.frame.sso.common.helper.SSOHelper;
import com.unitd.frame.sso.common.token.SSOToken;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @desc 首页控制器,控制首页显示或者判断是否登录,如果没有登录则直接转到登录页
 * @filename IndexController.java
 * @copyright www.unitd.com
 * @author Hudan
 * @version 1.0
 * @date 2016/10/4
 */
@Controller
public class IndexController extends BaseController {

	@Permission(action = Action.Skip)
	@RequestMapping("/")
	public String main(Model model) {
		// 获取当前请求Token
		SSOToken token = SSOHelper.getToken(request);

		// 判断是否登录,如果未登录则直接重定向到登录页,否则获取token中用户ID并转到首页
		if(BeanUtils.isNull(token)) {
			try {
				// 重定向到登录页面
				response.sendRedirect("/login");
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		} else {
			model.addAttribute("userId", token.getUid());
		}
		return "common/framework";
	}

	@Permission(action = Action.Skip)
	@RequestMapping("/index")
	public String index(Model model) {
		return "index";
	}
}